<template>
	<view class="wrap">
		<view class="top"></view>
		<view class="content">
			<view class="title">欢迎登录图书商城</view>
			<input class="u-border-bottom u-m-b-40" v-model="email" placeholder="请输入邮箱" />
			<input class="u-border-bottom u-m-b-40" type="password" v-model="password" placeholder="请输入密码" />
			<button @tap="submit" :style="[inputStyle]" class="getCaptcha">登陆</button>
			<view class="alternative">
				<view class="password" @tap="register">注册</view>
				<view class="issue">找回密码</view>
			</view>
		</view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			email: '',
			password: ''
		}
	},
	computed: {
		inputStyle() {
			let style = {};
			if(this.$u.test.email(this.email) && this.password) {
				style.color = "#fff";
				style.backgroundColor = this.$u.color['warning'];
			}
			return style;
		}
	},
	methods: {
		async submit() {
			// 校验输入信息
			if(!this.$u.test.email(this.email) || !this.password) return;
			// 处理登陆参数
			const params = {
				email: this.email,
				password: this.password
			}
			// 请求API，处理登陆
			const loginRes = await this.$u.api.authLogin(params);
			// 缓存token到本地
			this.$u.vuex('vuex_token', loginRes.access_token);
			this.$u.toast('登陆成功')
			
			// 获取用户信息
			this.$u.utils.refreshUserInfo()
			
			// 跳转返回来源页
			const backUrl = uni.getStorageSync('back_url');
			
			let backType = 'redirectTo';
			const barPathList = ['pages/index/index','pages/goods/goods','pages/cart/cart','pages/center/center']
			// 如果route路径为bar路径，则跳转方式用switchTab。
			if(barPathList.findIndex(item => item == backUrl) > -1) backType = 'switchTab';
			
			setTimeout(() => {
				this.$u.route({
					type: backType,
					url: backUrl
				})
			}, 1500)
		},
		
		register() {
			this.$u.route('/pages/auth/register')
		}
	}
};
</script>

<style lang="scss" scoped>
.wrap {
	font-size: 28rpx;
	.content {
		width: 600rpx;
		margin: 80rpx auto 0;

		.title {
			text-align: left;
			font-size: 60rpx;
			font-weight: 500;
			margin-bottom: 100rpx;
		}
		input {
			text-align: left;
			margin-bottom: 10rpx;
			padding-bottom: 6rpx;
		}
		.tips {
			color: $u-type-info;
			margin-bottom: 60rpx;
			margin-top: 8rpx;
		}
		.getCaptcha {
			background-color: rgb(253, 243, 208);
			color: $u-tips-color;
			border: none;
			font-size: 30rpx;
			padding: 12rpx 0;
			
			&::after {
				border: none;
			}
		}
		.alternative {
			color: $u-tips-color;
			display: flex;
			justify-content: space-between;
			margin-top: 30rpx;
		}
	}
}
</style>
